package com.keeley.kit;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import com.google.gson.Gson;
import com.jfinal.log.Logger;
import com.keeley.content.Content;
import com.keeley.exception.TiebaException;
import com.keeley.model.Vest;
import com.keeley.model.VestItem;

public class TieBaKit {
	private static Logger log = Logger.getLogger(TieBaKit.class);
	
	public static String getTbs(String cookie) throws TiebaException{
		String result =PostKit.sendGet("http://tieba.baidu.com/dc/common/tbs","",cookie);
		TbsJson tbsJoson= new Gson().fromJson(result, TbsJson.class);
		if(tbsJoson.is_login==0){
			throw new TiebaException(0,"登录失败");
		}
		return tbsJoson.tbs;
	}
	@SuppressWarnings("deprecation")
	public static String getCookieByBudss(String bduss){
        return 	"BDUSS=" + bduss + ";domain=baidu.com;path=/;expires="
                + new Date().toGMTString();
    }
	
	class TbsJson{
		int no;
		int err_code;
		int error_code;
		private String tbs;
		private int is_login;
	}
	
	/**
	 * 添加关注
	 * @return
	 */
	public static int likeAdd(VestItem item,Vest vest){
		try {
			String cookie = getCookieByBudss(item.getStr("bduss"));
			String url="http://tieba.baidu.com/f/like/commit/add";
			String tbs=getTbs(cookie);
			String tbName = item.getStr("forumName");
			StringBuffer param =new StringBuffer();
			param.append("fid=").append(item.getInt("forumid"));
			param.append("&fname=").append(tbName);
			param.append("&ie=").append("gbk");
			param.append("&tbs=").append(tbs);
			param.append("&uid=").append( URLEncoder.encode(vest.getStr("name"),"gbk") );
			
			String result =PostKit.sendPost(url,param.toString(),cookie);
			
			log.debug("likeAdd result:"+result);
			int no=new Gson().fromJson(result, TbsJson.class).no;
			return no;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	} 
	public static int poll(VestItem item,Vest vest) throws TiebaException{
		String result="";
		try {
			String cookie = getCookieByBudss(vest.getStr("bduss"));
			String tbs=getTbs(cookie);
			
			String parm="";
			String url="";
			int type = item.getInt("type");
			if(type==0) {
				parm= getStParam(tbs,item);
				url="http://tieba.baidu.com/actstage/photoVote?";
			}else if(type==1) {
				parm= getCJParam(tbs,item);
				url="http://tieba.baidu.com/actstage/lottery";
			}else if(type==2) {
				parm= getTpParam(tbs,item);
				url="http://tieba.baidu.com/actstage/photoVote?";
			}else{
				log.error("类型错误");
				return Content.ERROR_SERICE;
			}
			
			result=PostKit.sendPost(url,parm, cookie);
			log.info(result);
			int no=new Gson().fromJson(result, TbsJson.class).no;
			return no;
		}catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return Content.ERROR_SERICE;
	}
	/**
	 * 晒图
	 * @param tbs
	 * @param item
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getStParam(String tbs,VestItem item) throws UnsupportedEncodingException{
		StringBuffer param=new StringBuffer();
		
		param.append("tbs="+tbs);
		param.append("&activity_id="+item.get("activity"));
		param.append("&post_id="+item.get("postid"));
		param.append("&user_id_ed="+item.getStr("userid"));
		param.append("&user_name_ed="+URLEncoder.encode(item.getStr("username"), "utf-8"));
		param.append("&word="+URLEncoder.encode(item.getStr("forumName"), "utf-8"));
		param.append("&thread_id="+item.get("threadid"));
		param.append("&forum_id="+item.get("forumid"));
		
		return param.toString();
	}
	/**
	 * 投票
	 * @param tbs
	 * @param item
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getTpParam(String tbs,VestItem item) throws UnsupportedEncodingException{
		StringBuffer param=new StringBuffer();
		
		param.append("tbs="+tbs);
		param.append("&vote_id="+item.get("voteId"));
		param.append("&selected_items="+item.get("selectedItems"));
		param.append("&product_id="+item.getStr("userid"));
		
		return param.toString();
	}
	/**
	 * 抽奖
	 */
	public static String getCJParam(String tbs,VestItem item) throws UnsupportedEncodingException{
		StringBuffer param=new StringBuffer();
		
		param.append("tbs="+tbs);
		param.append("&thread_id="+item.get("threadid"));
		param.append("&forum_id="+item.get("forumid"));
		param.append("&activity_id="+item.getStr("activity"));
		
		return param.toString();
	}
	
}
